package com.episode.duetapi.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.core.MediaType;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.episode.duetapi.model.Admin;
import com.episode.duetapi.model.User;
import com.episode.duetapi.service.AdminService;
import com.episode.duetapi.utils.ResultTools;

@RestController
public class LoginController {
	
	@Autowired
	private AdminService adminService;
	
	@RequestMapping(value="/login",method=RequestMethod.POST,consumes=MediaType.APPLICATION_FORM_URLENCODED)
	public Object login(String userName,String password,String vcode,boolean rememberMe) {
		UsernamePasswordToken token = new UsernamePasswordToken(userName,password,rememberMe);
		SecurityUtils.getSubject().login(token);
		Subject subject = SecurityUtils.getSubject();
		Admin admin = (Admin) subject.getPrincipal();
		admin.setPassword(null);
		//request.getSession().setAttribute("user", admin);
		return ResultTools.getDataResult(admin);
	}
	
	@RequestMapping(value="/index",method=RequestMethod.GET)
	public Object home() {
		Subject subject = SecurityUtils.getSubject();
		User principal = (User) subject.getPrincipal();
		return principal;
	}

}
